﻿using Home.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Utilities.Validation;
using Omu.ValueInjecter;
using Utilities.ViewModel;

namespace Home.WebUI.Areas.Mythi.Controllers
{
    [Home.WebUI.Areas.NewAdmin.Models.AdminLogin]
    public class SingleController : Controller
    {
        #region 基本
        private EFDbContext db = new EFDbContext();
        private DbSet<SingleData> MEntity
        {
            get { return db.SingleDatas; }
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        #endregion
        //
        // GET: /Mythi/Single/

        public ActionResult Index(string singlename)
        {
            switch (singlename)
            {
                case "Company":
                    ViewBag.Title = "公司简介";
                    break;
                case "Culyure":
                    ViewBag.Title = "企业文化";
                    break;
                case "Services":
                    ViewBag.Title = "服务范围";
                    break;
                case "Contact_us":
                    ViewBag.Title = "联系我们";
                    break;
                case "Recruitment":
                    ViewBag.Title = "招聘信息";
                    break;
            }
            ViewBag.SidebarId = singlename;
            SingleData content = new SingleData();
            if (MEntity.Where(t=>t.SingleName==singlename).Count()==0)
            {
                
                content.SingleName = singlename;
                db.SingleDatas.Add(content);
                db.SaveChanges();
            }
            else
            {
                content = MEntity.First(t => t.SingleName == singlename);
            }
            SingleCreate model = new SingleCreate();
            model.InjectFrom(content);
            return View(model);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Index(string singlename,SingleCreate model)
        {
            if (model.UniValidate.CheckAll(Request))
            {
                var content = new SingleData { Id = model.Id };
                MEntity.Attach(content);
                content.InjectFrom(model);
                db.SaveChanges();
                TempData["Tip"] = new TipModel(1, "保存成功");
                return RedirectToAction("Index", new { singlename = singlename });
            }
            //}
            //catch (DataException)
            //{
            //    TempData["Tip"] = new TipModel(0, "无法保存更改。再试一次，和如果问题仍然存在，请联系您的系统管理员");
            //}
            return View(model);
        }
    }

        #region 验证
        public class SingleCreate
        {
            public int Id { get; set; }
            public string Title { get; set; }
            public string Body { get; set; }
            public string SingleName { get; set; }
            public UniValidate UniValidate
            {
                get
                {
                    var uv = new UniValidate();
                    uv.InitName(() => Body).NotEmpty("不能为空");
                    uv.ValidEnd(".form-horizontal");
                    return uv;
                }
            }
        }
        #endregion
}
